//index.js
const app = getApp()

Page({
    data: {
        // iphoneType:'iPhone X'
        iphoneType: 'iPhone 7',
        hidden: false
    },
    onLoad: function () {
        // this.getUserInfo();
        const extraData = wx.getStorageSync('extraData');
        const model = wx.getStorageSync('model');
        const devicesType = wx.getStorageSync('devicesType');
        this.setData({
            iphoneType: model,
        });
        if (extraData === undefined || extraData === "") {
            this.changeLoadingState(true);
            return;
        }
        const header = {
            'content-type': 'application/x-www-form-urlencoded',
            'accept': 'application/json',
            'Game-ID': extraData.game_id,
            'Game-Auth-Code': extraData.game_auth_code
        };
        if (devicesType === 'android') {
            this.wxlogn(header, extraData);
        }
    },
    wxlogn: function (headers, extraData) {
        wx.login({
            success: res => {
                this.createOrder(headers, extraData, res)
            },
            fail: res => {
                this.fail(res);
            }
        });
    },
    createOrder: function (header, extraData, res) {
        const code = res.code;
        const params = {
            game_id: extraData.game_id,
            open_id: extraData.open_id,
            quantity: extraData.quantity,
            payment_code: code,
            outer_order_no: extraData.outer_order_no,
        };
        if (extraData.midas_zone_id !== undefined) {
            params.midas_zone_id = extraData.midas_zone_id;
        }
        if (extraData.remark !== undefined) {
            params.remark = extraData.remark;
        }
        if (extraData.ext !== undefined) {
            params.ext = extraData.ext;
        }
        const url = extraData.isDebug ? 'https://apitest.9377.com' : 'https://api.9377.com';
        wx.request({
            url: `${url}/api/gateway/wechat/miniprogram/order/create`,
            method: 'POST',
            header: header,
            data: params,
            success: res => {
                if (res.data === undefined || res.data.code !== 1) {
                    this.failCallback(res);
                } else {
                    this.requestPayment(header, extraData, res);
                }
            },
            fail: res => {
                this.failCallback(res);
            }
        });
    },
    requestPayment: function (header, extraData, resOrder) {
        const params = {
            timeStamp: resOrder.data.data.timeStamp,
            nonceStr: resOrder.data.data.nonceStr,
            package: resOrder.data.data.package,
            paySign: resOrder.data.data.paySign,
            signType: resOrder.data.data.signType,
            success: res => {
                this.confirmOrder(header, extraData, resOrder)
            },
            fail: res => {
                this.failCallback(res);
            }
        };
        wx.requestPayment(params);
    },
    confirmOrder: function (header, extraData, res) {
        const params = {
            game_id: extraData.game_id,
            open_id: extraData.open_id,
            order_no: res.data.data.order_no,
        };
        if (extraData.midas_zone_id !== undefined) {
            params.midas_zone_id = extraData.midas_zone_id
        }
        const url = extraData.isDebug ? 'https://apitest.9377.com' : 'https://api.9377.com';
        wx.request({
            url: `${url}/api/gateway/wechat/miniprogram/order/confirm`,
            method: 'POST',
            header: header,
            data: params,
            success: res => {
                if (res.data !== undefined && (res.data.code === 1 || res.data.code === 80001 || res.data.code === 80002)) {
                    res.data.msg = '订单确认成功';
                    wx.navigateBackMiniProgram({extraData: {res: res, type: 'switchWxPay', state: 'success'}});
                } else {
                    this.failCallback(res);
                }
            },
            fail: res => {
                this.failCallback(res)
            }
        });
    },
    failCallback: function (res) {
        wx.navigateBackMiniProgram({extraData: {res: res, type: 'switchWxPay', state: 'fail'}});
        this.changeLoadingState(true);
    },
    changeLoadingState: function (ishidden) {
        this.setData({
            hidden: ishidden,
        })
    },
    // 获取用户信息
    getUserInfo: function () {
        wx.getSetting({
            success: res => {
                console.log(JSON.stringify(res));
                if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getSystemInfo({
                        success: res => {
                            this.setData({
                                iphoneType: res.model,
                            })
                        }
                    })
                }
            }
        });
    }
});
